package com.gientech.springexercise.controller.rocketMQ;

import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author zhangfantong
 * @Date 2023/2/25 00:27
 * @Version 1.0
 */
@RestController
@RocketMQMessageListener(topic = "user1", consumerGroup = "my-consumer-group")
public class UserController implements RocketMQListener<String> {
    String user;
    String key = "user21";

    @Resource
    RedisTemplate redisTemplate;

    @Override
    public void onMessage(String s) {
        System.out.println("User@@@@@接收信息："+s);
        if ( s!= null){
            user = s;
            redisTemplate.opsForValue().set(key, s);
        }
    }
    @GetMapping("/getUser")
    public String getUser(){
        Object o = redisTemplate.opsForValue().get(key);
        String s = o + "";
        if (!"".equals(s)){
            return "用户信息为:"+s;
        }
        return "用户还没注册";
    }
}
